//
//  BaseTableViewController.swift
//  Weibo
//
//  Created by 5201-mac on 2017/4/15.
//  Copyright © 2017年 wlb-mac. All rights reserved.
//

import UIKit

class BaseTableViewController: UITableViewController {
 
    //通过判断本地是否有token来判断用户是否登录
    var isLogin = UserAccount.loadAccount()?.access_token != nil
    
    override func loadView() {
        //可以在这个方法中自定根视图
        if isLogin {
            //加载tableView
            super.loadView()
        } else {
            //设置访客视图
            setVisitorView()
            //设置导航栏
            setNavBar()
        }
    }
    
    //MARK: 设置访客视图
    func setVisitorView() {
        let tipLabel = UILabel()
        //设置提示文字
        tipLabel.text = "请先登录"
        tipLabel.font = UIFont.systemFont(ofSize: 30)
        tipLabel.textAlignment = .center
        tipLabel.textColor = UIColor.darkGray
        //子类的对象指向父类的引用
        view = tipLabel
    }
    
    //设置导航栏
    func setNavBar() {
        //修改文字显示颜色
        self.navigationController?.navigationBar.tintColor = UIColor.orange
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(BaseTableViewController.registerDidClick))
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(BaseTableViewController.loginDidClick))
    }
    
    //MARK: 监听事件
    func registerDidClick() {
        print(#function)
    }
    
    func loginDidClick() {
        print(#function)
        //跳转到对应的登录界面，使用的是新浪微博的第三方认证登录页面
        //手写代码创建的视图控制器的根视图的背景颜色是透明的
        let oauth = OAuthViewController()
        let nav = UINavigationController(rootViewController: oauth)
        //self.navigationController?.pushViewController(oauth, animated: true)
        
        //应用程序的交互原理
        //如果两个页面之间没有关联性，不推荐使用push的方式，苹果更加推荐使用模态窗口的形式进行页面跳转
        self.present(nav, animated: true, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 1
    }

    /*
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)

        // Configure the cell...

        return cell
    }
    */

    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
